﻿using System.Net;
using System.Text;
using Zinnia.Extensions;

namespace Zinnia.Web.Common
{
    /// <summary>
    /// 模拟请求
    /// </summary>
    public static class HttpHelper
    {
        /// <summary>
        /// 模拟POST【普通请求】
        /// </summary>
        /// <param name="requestUrl"></param>
        /// <param name="postData"></param>
        /// <param name="contentType"></param>
        /// <returns></returns>
        public static byte[] Post(string requestUrl, byte[] postData, string contentType = "application/x-www-form-urlencoded")
        {
            var request = (HttpWebRequest)WebRequest.Create(requestUrl);
            request.Method = "POST";
            request.ContentType = contentType;
            using (var requestStream = request.GetRequestStream())
            {
                requestStream.Write(postData, 0, postData.Length);
                using (var response = request.GetResponse())
                {
                    using (var responseStream = response.GetResponseStream())
                    {
                        return responseStream.ReadBytes();
                    }
                }
            }
        }
        public static string PostUtf8(string requestUrl, byte[] postData, string contentType = "application/x-www-form-urlencoded")
        {
            return Encoding.UTF8.GetString(Post(requestUrl, postData, contentType));
        }
        public static string Post(string requestUrl, byte[] postData, Encoding encoding, string contentType = "application/x-www-form-urlencoded")
        {
            encoding = encoding.NotNull("encoding");
            return encoding.GetString(Post(requestUrl, postData, contentType));
        }
        /// <summary>
        /// 模拟Get【带请求头以及响应头信息】
        /// </summary>
        /// <param name="requestUrl"></param>
        /// <param name="responseHeaders"></param>
        /// <returns></returns>
        public static byte[] Get(string requestUrl, out WebHeaderCollection responseHeaders)
        {
            var request = (HttpWebRequest)WebRequest.Create(requestUrl);
            using (var response = request.GetResponse())
            {
                using (var responseStream = response.GetResponseStream())
                {
                    responseHeaders = response.Headers;
                    return responseStream.ReadBytes();
                }
            }
        }
        /// <summary>
        /// 模拟Get请求
        /// </summary>
        /// <param name="requestUrl"></param>
        /// <returns></returns>
        public static byte[] Get(string requestUrl)
        {
            WebHeaderCollection webHeaderCollection;
            return Get(requestUrl, out webHeaderCollection);
        }

        public static string GetUtf8(string requestUrl)
        {
            return Encoding.UTF8.GetString(Get(requestUrl));
        }
        public static string GetUtf8(string requestUrl, Encoding encoding)
        {
            encoding = encoding.NotNull("encoding");
            return encoding.GetString(Get(requestUrl));
        }
        public static string GetUtf8(string requestUrl, out WebHeaderCollection reponseHeaders)
        {
            return Encoding.UTF8.GetString(Get(requestUrl, out reponseHeaders));
        }
        public static string Get(string requestUrl, Encoding encoding, out WebHeaderCollection reponseHeaders)
        {
            encoding = encoding.NotNull("encoding");
            return encoding.GetString(Get(requestUrl, out reponseHeaders));
        }
    }
}
